<?php


namespace app\sys\model;

use think\facade\Cache;
use think\facade\Request;
use think\Model;
use Kqtec\IpLocationZh\Ip;

class StaffActionLog extends Model
{
    protected $autoWriteTimestamp = true;
    //写入日志
    //response_status 成功or失败
    //response_msg 简述
    //remark 详细描述
    public static function write($response_status, $response_msg, $remark = "",$request_data = "")
    {
        $start = Request::time(true) * 1000;
        $ip = Request::ip();
        $address = implode("/", Ip::find($ip)["location"]);
        $user = Request::param("_user");
        $staff_no = "";
        $staff_name = "";
        if (isset($user->staff_no) && $user->staff_no != null) {
            $staff_no = $user->staff_no;
            $staff_name = $user->staff_name;
        }
        $name = Cache::get('uri_' . Request::path());
        $ms = msectime() - intval($start);
        $info = [
            "staff_no" => $staff_no,
            "name" => $name,
            "staff_name" => $staff_name,
            "url" => Request::baseUrl(),
            "ip" => $ip,
            "address" => $address,
            "method" => Request::method(),
            "ms" => $ms,
            "response_status" => $response_status,
            "response_msg" => $response_msg,
            "remark" => $remark,
            "request_body" => Request::except("_user,_uuid"),
            "uuid" => Request::param("_uuid"),
            "http_status" => 200,
            "action" => Request::action(),
            "user_agent" => Request::header("user-agent"),
            "request_data" => $request_data,
        ];
        StaffActionLog::create($info);
    }

    // 记录

    /**
     * @param $typeMethod
     * @param $page
     * @param $limit
     * @return StaffActionLog[]|\think\Collection
     */
    public static function loginLog($typeMethod,$page,$limit)
    {
        $logActSql = StaffActionLog::where("action",$typeMethod)
            ->field("address,ip,create_time,user_agent,id")
            ->where("response_status",1);
        $logActC = $logActSql->count();
        $logAct =  $logActSql->order("id desc")->page($page,$limit)
            ->select();
        return listDate($logAct,$logActC);
    }

    /**
     * @param $typeMethod
     * @param $page
     * @param $limit
     * @return StaffActionLog[]|\think\Collection
     */
    public static function ActLog($typeMethod,$date,$data,$page,$limit)
    {
        $logActSql = StaffActionLog::where("action","<>",$typeMethod)
            ->field("staff_no,staff_name,create_time,ip,response_msg")
            ->where("response_status",1);

        if(isset($data["staff_id"])){
            $logActSql = $logActSql->where("staff_id",$data["staff_id"]);
        }

        if( (!empty($date)) && isset($data[0])&&isset($data[1])){
            $logActSql = $logActSql
                ->whereBetweenTime("create_time",strtotime($data[0]),strtotime ($data[1]));
        }
        $logActC = $logActSql->count();
        $logAct = $logActSql->order("create_time desc")->page($page,$limit)
            ->select();
        return listDate($logAct,$logActC);
    }
}